package order_service

import (
	"bee/pkg/logging"
	"bee/pkg/util"
	"errors"
	"github.com/go-resty/resty/v2"
	"time"
)

func getUserInfo(userType int) map[string]string {
	userInfo := make(map[string]string)

	switch userType {
	case 1:
		userInfo["loginName"] = "xxbyw_hb"
		userInfo["password"] = "a5eGNsovdGY="
		userInfo["rememberMe"] = "false"
		return userInfo
	case 2:
		userInfo["loginName"] = "xqy_admin"
		userInfo["password"] = "a5eGNsovdGY="
		userInfo["rememberMe"] = "false"
		return userInfo

	default:
		userInfo["loginName"] = "xxbyw_hb"
		userInfo["password"] = "a5eGNsovdGY="
		userInfo["rememberMe"] = "false"
		return userInfo
	}
}

func (o Order) GetBossToken(host string) (string, error) {
	uri := "/xqy-portal-web/boss/login/base/login"
	url := host + uri

	loginUser := getUserInfo(o.UserType)

	logging.ZapLogger.Infow("[boss登录]","url", url, "登录用户信息", loginUser, "traceId", o.TraceId)

	client := resty.New()
	client.SetTimeout(5 * time.Second)
	resp, err := client.R().SetFormData(loginUser).Post(url)
	logging.ZapLogger.Infow("【boss登录】", "响应结果", resp)
	if err != nil {
		logging.ZapLogger.Infow("[boss登录]", "错误信息", err)
		return "", err
	}

	data := util.GetCookiesToMap(resp.Cookies())

	token := data["sso-epctoken"]
	if token == "" {
		return "", errors.New("sso-epctoken不存在，请检查")
	}

	logging.ZapLogger.Infow("[boos登录]", "获取的token", token)
	return token, nil
}

